package com.sas.hnn;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class PartyEdit extends Activity {

	/**
	 * @uml.property  name="tAG"
	 */
	private final String TAG = PartyEdit.class.getSimpleName();

	/**
	 * @uml.property  name="mDbHelper"
	 * @uml.associationEnd  
	 */
	private PicsDbAdapter mDbHelper;
	/**
	 * @uml.property  name="mWahl"
	 */
	private String mWahl;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.grid_view);
		Log.d(TAG,"in onCreate()");
		mDbHelper = new PicsDbAdapter(this);
		mDbHelper.open();
		// get Extras from Gesinnung Activity
		mWahl = (savedInstanceState == null) ? null
				: (String) savedInstanceState
						.getSerializable(PicsDbAdapter.KEY_WAHL);

		Log.d(TAG, "mWahl von savedInstanceState.getSerializable '"+mWahl+"'");
		if (mWahl == null) {
			Bundle extras = getIntent().getExtras();
			mWahl = extras != null ? extras.getString(PicsDbAdapter.KEY_WAHL)
					: null;
			Log.d(TAG, "mWahl von getExtras '"+mWahl+"'");
		}
		GridView gridview = (GridView) findViewById(R.id.gridview);
		gridview.setAdapter(new ImageAdapter(this));

		gridview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {

				changeParty(parent, v, position, id);
				setResult(RESULT_OK);
				finish();				
//				Intent i = new Intent(PartyEdit.this,Gesinnung.class);
//				startActivity(i);			
				
				
				
			}
		});
		
	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG,"in onStart()");
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG,"in onResume()");
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.d(TAG,"in onPause()");
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.d(TAG,"in onStop()");
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG,"in onDestroy()");
		mDbHelper.close();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.d(TAG,"in onRestart()");
	}

	private void changeParty(AdapterView<?> parent, View v, int position,
			long id) {

		Resources r = getResources();
		mDbHelper.updateWahl(mWahl,
				r.getResourceEntryName(ImageAdapter.mThumbIds[position]));

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
		Log.d(TAG,"in onSaveInstanceState()");
        outState.putSerializable(PicsDbAdapter.KEY_WAHL, mWahl);
	}

}
